package com.fosu.Utils;



import com.fosu.common.ResponseResult;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author zqa
 * @version 1.0
 * @date 2020/11/12 22:53
 */
public class Md5Util {

    /**
     * MD5加密
     * @param message 待加密的内容
     * @return 加密后的字符串 (32位)
     */
    public static String getMD5Str(String message) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
            md.reset();
            md.update(message.getBytes("UTF-8"));

        } catch (NoSuchAlgorithmException e) {

            System.out.println("MD5加密异常:" + e);
        } catch (UnsupportedEncodingException e) {

            System.out.println("MD5加密异常:" + e);
        }
        byte[] byteArray = md.digest();
        StringBuffer md5StrBuff = new StringBuffer();
        for (int i = 0; i < byteArray.length; ++i) {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
            } else {
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
            }
        }
        return md5StrBuff.toString();
    }
    }
